(function($) {

	var zIndex = 100;
	
	/**提示框*/
	$.message_alert = function(content,width,height){
		var option = {
				type: "warn",
				content: content,
				ok:"确定",
				cancel: null
			};
		
		if(width!=undefined && height!=undefined){
			option.width=width;
			option.height=height;
		}
		
		$.dialog(option);
	};
	
	/**确认框*/
	$.message_confirm = function(content,callback){
		$.dialog({
			type: "warn",
			content: content,
			onOk:callback
		});
	};
	
	//定时关闭的的弹框
	$.message_timer = function(type,content,time,callback){
		var $dialog = $.dialog({
			type: type,
			content: content,
			cancel: null,
			onClose:function(){
				if(callback != undefined && callback != null){
					if(callback()==false){
						return false;
					}
				}
			}
		});
		setTimeout(function(){
			$dialog.find(".dialogClose").click();
		},time);
	}

	// 对话框
	$.dialog = function(options) {
		var settings = {
			width: 320,
			height: "auto",
			modal: true,
			ok: '确&nbsp;&nbsp;定',
			cancel: '取&nbsp;&nbsp;消',
			onShow: null,
			onClose: null,
			onOk: null,
			onCancel: null
		};
		$.extend(settings, options);
		
		if (settings.content == null) {
			return false;
		}
		
		var closeIconHide_str = '';
		if (settings.closeIconHide == true) {
			closeIconHide_str = ' style="display:none;"';
		}
		
		var $dialog = $('<div class="xxDialog"><\/div>');
		var $dialogTitle;
		var $dialogClose = $('<div class="dialogClose" '+closeIconHide_str+'><\/div>').appendTo($dialog);
		var $dialogContent;
		var $dialogBottom;
		var $dialogOk;
		var $dialogCancel;
		var $dialogOverlay;
		if (settings.title != null) {
			$dialogTitle = $('<div class="dialogTitle"><\/div>').appendTo($dialog);
		}
		if (settings.type != null) {
			$dialogContent = $('<div class="dialogContent dialog' + settings.type + 'Icon"><\/div>').appendTo($dialog);
		} else {
			$dialogContent = $('<div class="dialogContent"><\/div>').appendTo($dialog);
		}
		if (settings.ok != null || settings.cancel != null || settings.bottomContent) {
			$dialogBottom = $('<div class="dialogBottom"><\/div>').appendTo($dialog);
		}
		if (settings.ok != null) {
			$dialogOk = $('<input type="button" class="button sureButton" value="' + settings.ok + '" \/>').appendTo($dialogBottom);
		}
		if (settings.cancel != null) {
			$dialogCancel = $('<input type="button" class="button cancleButton" value="' + settings.cancel + '" \/>').appendTo($dialogBottom);
		}
		if(settings.bottomContent!=null){
			$(settings.bottomContent).appendTo($dialogBottom);
		}
		
		if (!window.XMLHttpRequest) {
			$dialog.append('<iframe class="dialogIframe"><\/iframe>');
		}
		$dialog.appendTo("body");
		if (settings.modal) {
			$dialogOverlay = $('<div class="dialogOverlay"><\/div>').insertAfter($dialog);
		}
		
		var dialogX;
		var dialogY;
		if (settings.title != null) {
			$dialogTitle.text(settings.title);
		}
		$dialogContent.html(settings.content);
		var height = $(window).height();
		var width = $(window).width();
		$dialog.css({"width": settings.width, "height": settings.height, "left": (width/2)- parseInt(settings.width / 2),"top":(height/2)- parseInt(settings.height / 2)-30, "z-index": zIndex ++});
		var top = (height/2)- parseInt($dialog.height() / 2);
		if(top>30){
			top = top-30;
		}
		$dialog.css({"left": (width/2)- parseInt($dialog.width() / 2),"top":top});
		dialogShow();
		
		if ($dialogTitle != null) {
			$dialogTitle.mousedown(function(event) {
				$dialog.css({"z-index": zIndex ++});
				var offset = $(this).offset();
				if (!window.XMLHttpRequest) {
					dialogX = event.clientX - offset.left;
					dialogY = event.clientY - offset.top;
				} else {
					dialogX = event.pageX - offset.left;
					dialogY = event.pageY - offset.top;
				}
				$("body").bind("mousemove", function(event) {
					$dialog.css({"top": event.clientY - dialogY, "left": event.clientX - dialogX, "margin": 0});
				});
				return false;
			}).mouseup(function() {
				$("body").unbind("mousemove");
				return false;
			});
		}
		
		if ($dialogClose != null) {
			$dialogClose.click(function() {
				dialogClose(this);
				return false;
			});
		}
		
		if ($dialogOk != null) {
			$dialogOk.click(function() {
				$(this).prop("disabled",true);
				if (settings.onOk && typeof settings.onOk == "function") {
					if (settings.onOk(this,$dialog) != false) {
						dialogClose(this);
					}
				} else {
					dialogClose(this);
				}
				$(this).prop("disabled",false);
				return false;
			});
		}
		
		if ($dialogCancel != null) {
			$dialogCancel.click(function() {
				$(this).prop("disabled",false);
				if (settings.onCancel && typeof settings.onCancel == "function") {
					if (settings.onCancel(this,$dialog) != false) {
						dialogClose(this);
					}
				} else {
					dialogClose(this);
				}
				$(this).prop("disabled",false);
				return false;
			});
		}
		
		function dialogShow() {
			if (settings.onShow && typeof settings.onShow == "function") {
				if (settings.onShow($dialog) != false) {
					$dialog.show();
					$dialogOverlay.show();
				}
			} else {
				$dialog.show();
				$dialogOverlay.show();
			}
		}
		function dialogClose(e) {
			if (settings.onClose && typeof settings.onClose == "function") {
				if (settings.onClose(e,$dialog) != false) {
					$dialogOverlay.remove();
					$dialog.remove();
				}
			} else {
				$dialogOverlay.remove();
				$dialog.remove();
			}
		}
		return $dialog;
	}

})(jQuery);

/**新打开一个窗口（覆盖在当前页面上面）*/
function create_iframe(url,e){
	$(e).prop("display",true);
	var scrollTop = $(window).scrollTop();
	$("body").addClass("hide-body").attr("scrollTop",scrollTop);
	$(window).scrollTop(0);
	$('<iframe src="'+url+'" class="detail_page" style="position: absolute;top: 0px;left: 0px;width: 100%;height: 100%;z-index:9999"></iframe>').appendTo("html");
	$(e).prop("display",false);
}

/**关闭窗口*/
function cancle_iframe(){
	var $body = $("body",window.parent.document).removeClass("hide-body");
	var scrollTop = $body.attr("scrollTop");
	if(scrollTop!=undefined){
		$(parent.window).scrollTop(scrollTop);
	}
	$('.detail_page:last',window.parent.document).remove();
}


/**查询弹框*/
(function($) {
	var callQueryDialog = function (e,settings){
		if(settings.type==null || settings.url==null)return false;
		var iframeId = "iframeId" + (new Date()).valueOf() + Math.floor(Math.random() * 1000000);
		$this = $(e);
		var $dialog = $.dialog({
			title:settings.title,
			width:settings.width,
			height:settings.height,
			content: "<iframe  id='"+iframeId+"' src='"+settings.url+"' width='100%'  height='"+settings.iframe_height+"px'><\/iframe>",
			ok: "确定",
			cancel: "取消",
			onShow:function(){
				$this.prop("disabled",true);
			},
			onClose:function(){
				$this.prop("disabled",false);
			},
			onOk: function(btn) {
				$(btn).prop("disabled",true);
				var rows = $("#"+iframeId)[0].contentWindow.childMethod(btn);
				if(settings.callback != undefined && settings.callback != null){
					if(settings.callback(rows)==false){
						$(btn).prop("disabled",false);
						return false;
					}
				}else{
					if(rows.length>0){
						var row = rows[0];
						var $span = $this.parent("span");
						$span.find("input."+settings.type+"Id").val(row.id);
						$span.find("input."+settings.type+"Name").val(row.name);
						$span.find("input."+settings.type+"Sn").val(row.sn);
					}
				}
				$(btn).prop("disabled",false);
			}
		});
		//$dialog.css("top",settings.top+"px");
	
	}
	$.fn.bindQueryBtn =  function(options) {
		var settings = {
			title:"查询",
			width:1000,
			height:508,
			iframe_height:420,
			url:null,
			type:null,
			bindClick:true
		};
		$.extend(settings, options);
		//handleSelectDialogParam(settings);
		
		
		
		if(settings.bindClick){
			$(this).click(function(){
				callQueryDialog(this,settings);
			});
		}else{
			callQueryDialog(this,settings);
		}
	}
	/**
	function handleSelectDialogParam(settings){
		if(settings.type!=null && setting.url==null){
			if(settings.type=="warehouse"){
				settings.title = "查询仓库";
				settings.url = "/stock/warehouse/select_warehouse.jhtml";
			}else if(settings.type=="store"){
				settings.title = "查询客户";
				settings.url = "/member/store/select_store.jhtml";
			}else if(settings.type=="product"){
				settings.title = "查询产品";
				settings.url = "/product/product/selectProduct.jhtml";
			}
		}
	}*/

})(jQuery);


function error_win(message){
	var content = null;
    var exmsg =  message.objx;
    var rmid = '';
    if(message.rmid!=null){
   		rmid='('+message.rmid+')';
   	}
 
    content = rmid+"<br/>"+message.content;
    $.message_alert(content,500,243);
}

 function jumpTo(e,url,isBlank){
	if(isBlank){
		window.open(url);
	}else{
		$(e).prop("disabled",true);
		location.href=url;
	}
 }
 
 function excel_import(_this,options){
		var $btn = $(_this).prop("disabled",true);
		try{ 
			var settings ={
				title:null,
				url:null,
				template:null,
			}
			$.extend(settings, options);
			if(settings==null || settings.title==null || settings.url==null || settings.template==null){
				return false;
			}
			var browserFrameId = "browserFrame" + (new Date()).valueOf() + Math.floor(Math.random() * 1000000);
			var content = '<div>'
		    +'<div class="pup-cont" style="margin: 25px 25px 60px;">'
		    +'<iframe id="' + browserFrameId + '" name="' + browserFrameId + '" style="display: none;"><\/iframe>'
		    	+'<form action="' + settings.url + '" method="post" encType="multipart/form-data" target="' + browserFrameId + '">'
				+'<table class="input input-edit" style="width:100%">'
						+'<tbody><tr>'
							+'<th>'
								+'<span class="requiredField">*</span>选择Excel文件:'
							+'</th>'
							+'<td style="text-align:left">'
								+'<div class="upload-list atta-upload01">'
		                        	<!--上传完成换成这种 -->
									+'<div class="ul-box" style="display:none;"> '
									+'<div class="pic"><a href="javascript:void(0);"><img src="/resources/images/icon_xls.png" style="height:60px"></a></div> '
		    						+'<b class="">dfafdasf</b>'
									+'<p>fsdafdsafsaf</p>'
		    						+'<a class="del delProductImage" href="javascript:void(0);"></a>'
		    						+' </div>'
		                            <!--上传完成换成这种 -->
		                            +'<a href="javascript:;" class="a-upload"><input type="file" name="file" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /></a>'
								+'</div>'
							+'</td>'
						+'</tr>'
						+'<tr>'
						+'<th>'
							+'下载文件模板:'
						+'</th>'
						+'<td>'
							+'<input type="button" value="模板下载" class="button" onclick="window.location.href=\''+settings.template+'\'">'
						+'</td>'
					+'</tr>'
				+'</tbody></table></form>'
			+'</div>'
		+'</div>';
			var $content = $(content);
			var $uploadBtn = $content.find("a.a-upload > :file");
			var $ul_box = $content.find(".ul-box");
			var $form = $content.find("form");
			$uploadBtn.change(function(){
				var $this = $(this);
				var file = $this.val();
				var pos=file.lastIndexOf("\\");
		    	var fileName = file.substring(pos+1);
		    	
		    	var date = new Date();
				var year = date.getFullYear();
				var month = date.getMonth()+1;
				var day = date.getDate();
				var time = year+'-'+month+'-'+day;
		    	
		    	$ul_box.html('<div class="pic" style="width: 60px;height: 60px;"><a href="javascript:void(0);"><img src="/resources/images/icon_xls.png" style="height:60px"></a></div> '
				+'<b class="" title="'+fileName+'">'+abbreviate(fileName,15)+'</b>'
				+'<p>'+time+'</p>'
				+'<a class="del delProductImage" href="javascript:void(0);"></a> ');
		    	//
		    	//alert(fileName);
		    	$ul_box.find("a.del").click(function(){
		    		$uploadBtn.val("");
		    		$ul_box.html('').hide();
		    		$this.parent().show();
		    	});
		    	$ul_box.show();
		    	$this.parent().hide();
			});
			
			$.dialog({
				title: settings.title,
				content: $content,
				width:500,
				height:300,
				onShow:function(){
					$("iframe#"+browserFrameId).load(function() {
						var $iframe = $(this);	
						var text;
						var io = document.getElementById(browserFrameId);
						if(io.contentWindow) {
							text = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML : null;
						} else if(io.contentDocument) {
							text = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML : null;
						}
						
						if ($.trim(text) != "") {
						//text = text.substring(5,text.length-6);
						text = $(text).prop("innerText");
							var data = $.parseJSON(text);
							if (data.type == "success") {
								var $dialog = $form.closest(".xxDialog");
								$dialog.next(".dialogOverlay").andSelf().remove();
								$dialog.remove();
								if (settings.callback != null && typeof settings.callback == "function") {
									settings.callback(data.url);
								}else{
									$("#searchBtn").click();
								}
							} else {
								error_win(data);
							}
						}
					});
				},
				onClose:function(){
					$btn.prop("disabled",false);
				},
				onOk:function(){
					$form.submit();
					return false;
				}
			});
		}catch (e){ 
			$btn.prop("disabled",false);
			throw e;
		}
}


	/**选择导出*/
	function select_export(e,options){
		$(e).addClass("disabled").attr("disabled",true);
		var settings ={
			tip:null,
			param:null,
			url:null
		}
		$.extend(settings, options);
		if(settings.tip == null || settings.url==null){
			return false;
		}
		
		var id = settings.param;
	  	if(id==null || id==""){
	  		$.message_alert(settings.tip);
	  	}else{
	  		window.location.href = settings.url+'?'+ id;
	  	}
	  	$(e).removeClass("disabled").attr("disabled",false);
		
	}
	
	//条件导出
	function conditions_export(e,options){
		if($(e).hasClass("disabled")){
			return false;
		}
		var settings ={
			title:'分页导出',
			needConditions:true,
			page_url:null,
			url:null,
			data:null
		}
		$.extend(settings, options);
		if(settings.page_url==null || settings.url==null){
			return false;
		}
		
		var $conditions = $("#search-content").find("[name]");
		if(settings.needConditions){
			var isEmpty = true;
			for(var i=0;i<$conditions.length;i++){
				var $condition = $conditions.eq(i);
				if($condition[0].type=="checkbox"){
					if($condition.prop("checked") && $condition.val() != ""){
						isEmpty = false;
						break;
					}
				}else{
					if($conditions.eq(i).val() != ""){
						isEmpty = false;
						break;
					}
				}
				
			}
			if(isEmpty){
				$.message_alert('至少有一个搜索条件不为空');
				return false;
			}
		}
	
		$(e).addClass("disabled");
		var searchContent = $conditions.serialize();
		if(settings.data!=null){
			searchContent=settings.data;
		}
		$.ajax({
			url:settings.page_url,
			type:"post",
			data:searchContent,
			dataType:"json",
			cache:false,
			success:function(lists){
				var exportTableId = "exportTable" + (new Date()).valueOf() + Math.floor(Math.random() * 1000000);
				var total=lists[lists.length-1].data.split("-")[1];
				var table='<table id="'+exportTableId+'" class="input exportTable" style="width:100%;border:1px solid #e9e9e9;margin-top:0px;margin-bottom:0px;" collapse><tr class="title"><th width="50%">分页</th><th width="50%" style="text-align:center">操作</th></tr>';
				for(var i=0;i<lists.length;i++){
					var  d= lists[i];
					var n=parseInt(i+1);
					table += '<tr><td>第'+n+'页('+d.data+')</td><td style="text-align:center;"><input type="button" value="导出此页" class="button exportBtn" page="'+n+'"/></td></tr>';
				}
				table+='</table>';
				var content='<div style="padding:10px;height:355px;overflow-y:auto;"><p>共查询出<span>'+total+'</span>条数据，每次导出页最多1000条，请点击你需要导出的分页数据！</p><div class="tableBox">'+table+'</div></div>';
				$.dialog({
					title:'条件查询分页导出',
					content:content,
					width:520,
					height:460,
					ok:null,
					cancel:'返回',
					onShow:function(){
						$("#"+exportTableId+" .exportBtn").click(function(){
							$(this).addClass("disabled").attr("disabled",true);
							var page = $(this).attr("page");
							var param = searchContent+"&page="+page;
							var url = "";
							if(settings.url.indexOf("?")>0){
								url = settings.url+"&"+param;
							}else{
								url = settings.url+"?"+param;
							}
							
							location.href=url;
							$(this).attr("disabled",false);
						});
						
					},
					onCancel:function(){
						$(e).removeClass("disabled");
					},
					onClose:function(){
						$(e).removeClass("disabled");
					}
				})
			}
		})
		
	}
	